package com.se2.antiqueexhibition.Controller;

import com.se2.antiqueexhibition.Service.RelicCategoryService;
import com.se2.antiqueexhibition.pojo.RelicCategory;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin/relicCategory")
public class RelicCategoryController {

    @Resource
    private RelicCategoryService relicCategoryService;

    /**
     * 显示分类列表页面
     */
    @GetMapping("/list")
    public String list(Model model) {
        List<RelicCategory> categories = relicCategoryService.getAllCategories();
        model.addAttribute("categories", categories);
        return "relicCategory-management";
    }

    /**
     * 显示添加分类表单页面
     */
    @GetMapping("/add")
    public String showAddForm(Model model) {
        model.addAttribute("category", new RelicCategory());
        model.addAttribute("isEdit", false);
        return "relicCategory-form";
    }

    /**
     * 显示编辑分类表单页面
     */
    @GetMapping("/edit")
    public String showEditForm(@RequestParam int id, Model model) {
        RelicCategory category = relicCategoryService.getCategoryById(id);
        if (category == null) {
            model.addAttribute("message", "分类不存在");
            model.addAttribute("success", false);
            return "redirect:/admin/relicCategory/list";
        }
        model.addAttribute("category", category);
        model.addAttribute("isEdit", true);
        return "relicCategory-form";
    }

    /**
     * 添加分类
     */
    @PostMapping("/add")
    public String add(@ModelAttribute RelicCategory category, Model model) {
        try {
            // 验证名称不为空
            if (category.getName() == null || category.getName().trim().isEmpty()) {
                model.addAttribute("message", "分类名称不能为空");
                model.addAttribute("success", false);
                model.addAttribute("category", category);
                model.addAttribute("isEdit", false);
                return "relicCategory-form";
            }
            // 检查名称是否已存在
            if (relicCategoryService.isNameExists(category.getName())) {
                model.addAttribute("message", "分类名称已存在，请使用其他名称");
                model.addAttribute("success", false);
                model.addAttribute("category", category);
                model.addAttribute("isEdit", false);
                return "relicCategory-form";
            }
            relicCategoryService.addCategory(category);
            model.addAttribute("message", "分类添加成功");
            model.addAttribute("success", true);
            return "redirect:/admin/relicCategory/list";
        } catch (Exception e) {
            model.addAttribute("message", "分类添加失败：" + e.getMessage());
            model.addAttribute("success", false);
            model.addAttribute("category", category);
            model.addAttribute("isEdit", false);
            return "relicCategory-form";
        }
    }

    /**
     * 更新分类
     */
    @PostMapping("/update")
    public String update(@ModelAttribute RelicCategory category, Model model) {
        try {
            // 验证名称不为空
            if (category.getName() == null || category.getName().trim().isEmpty()) {
                model.addAttribute("message", "分类名称不能为空");
                model.addAttribute("success", false);
                model.addAttribute("category", category);
                model.addAttribute("isEdit", true);
                return "relicCategory-form";
            }
            // 检查名称是否被其他分类使用
            RelicCategory existingCategory = relicCategoryService.getCategoryById(category.getId());
            if (existingCategory == null) {
                model.addAttribute("message", "分类不存在");
                model.addAttribute("success", false);
                return "redirect:/admin/relicCategory/list";
            }
            
            // 如果名称改变，检查新名称是否已存在
            if (!existingCategory.getName().equals(category.getName()) 
                && relicCategoryService.isNameExists(category.getName())) {
                model.addAttribute("message", "分类名称已存在，请使用其他名称");
                model.addAttribute("success", false);
                model.addAttribute("category", category);
                model.addAttribute("isEdit", true);
                return "relicCategory-form";
            }
            
            relicCategoryService.updateCategory(category);
            model.addAttribute("message", "分类更新成功");
            model.addAttribute("success", true);
            return "redirect:/admin/relicCategory/list";
        } catch (Exception e) {
            model.addAttribute("message", "分类更新失败：" + e.getMessage());
            model.addAttribute("success", false);
            model.addAttribute("category", category);
            model.addAttribute("isEdit", true);
            return "relicCategory-form";
        }
    }

    /**
     * 删除分类
     */
    @GetMapping("/delete")
    public String delete(@RequestParam int id, Model model) {
        try {
            relicCategoryService.deleteCategory(id);
            model.addAttribute("message", "分类删除成功");
            model.addAttribute("success", true);
        } catch (Exception e) {
            model.addAttribute("message", "分类删除失败：" + e.getMessage());
            model.addAttribute("success", false);
        }
        return "redirect:/admin/relicCategory/list";
    }
}

